package com.sgb.kjwl.viewmodel.self.packers;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.EmploymentEntity;
import com.sgb.kjwl.model.entity.self.SelfEmploymentEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamEmploymentActivity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SelfTeamEmploymentModel extends BaseViewModel {
    public ObservableField<Map<String,Object>> map = new ObservableField<>(new HashMap<>());
    private SelfTeamEmploymentActivity activity;

    private int page = 1;
    private int limit = 10;

    public SelfTeamEmploymentModel(SelfTeamEmploymentActivity activity) {
        this.activity = activity;
    }

    public void getEmploymentList(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        map.get().clear();
        map.get().put("page", page);
        map.get().put("limit", limit);
        // 首选企业编号，如果企业编号为空，则传用户编号，两者不能同时为空，不能同时都有值
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        int userType = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        if ("".equals(companyNo) || "0".equals(companyNo)) {
            map.get().put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        } else if (userType == 2){
            map.get().put("teamNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, ""));
        }else {
            map.get().put("companyNo", companyNo);
        }
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getEmploymentList(map.get()).enqueue(new ICallBack<BaseEntity<ListResponse<List<EmploymentEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<EmploymentEntity>>> data) {
                if (data.isSuccess()) {
                    activity.showList(data.getData().getList(), isLoadMore, data.getData().getList() == null || data.getData().getList().size() < limit);
                }
            }
        });
    }

    public void delEmployment(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        map.get().clear();
        map.get().put("no", no);
        APIManager.getInstance().getUserAPI().deleteEmployment(map.get()).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    MToast.showToast(activity,"删除成功");
                    getEmploymentList(false);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    /**
     * 查看详情
     * @param no
     */
    public void getH5Url(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("workforce").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + no + "&isdele=1";
                    WebViewX5HostActivity.startForEmploymentRelease(activity, url,"用工详情");
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
